#include "BlockQueue.hpp"
#include "Task.hpp"

void *Productor(void *arg)
{
    BlockQueue<Task> *bq = reinterpret_cast<BlockQueue<Task> *>(arg);
    // 生产者不断进行生产
    while (true)
    {
        // 获取数据
        // sleep(1);
        int a = rand() % 10 + 1;
        int b = rand() % 20 + 1;
        Task t(a, b);
        // 生产任务
        bq->Push(t);
        std::cout << t.DebugToString() << std::endl;
        ;
        // std::cout << "Productor: " << data << std::endl;
    }
}

void *Consumer(void *arg)
{
    BlockQueue<Task> *bq = reinterpret_cast<BlockQueue<Task> *>(arg);
    // 消费者不断进行消费
    while (true)
    {
        // 获取任务
        sleep(1);
        Task t;
        bq->Pop(&t);
        // 处理任务
        t();
        std::cout << t.ResultToString() << std::endl;
        // std::cout << "Consumer: " << data << std::endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    pthread_t productor, consumer;
    BlockQueue<Task> *bq = new BlockQueue<Task>;
    // 创建生产者线程和消费者线程，单生产单消费模型
    pthread_create(&productor, nullptr, Productor, bq);
    pthread_create(&consumer, nullptr, Consumer, bq);

    // Join生产者线程和消费者线程
    pthread_join(productor, nullptr);
    pthread_join(consumer, nullptr);
    delete bq;
    return 0;
}